package com.ningxia.water.configuration;

import org.springframework.context.annotation.Bean;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import static java.lang.Double.parseDouble;
import static java.lang.StrictMath.sqrt;
public class PredictLiner {
    public static <T> T[] concatAll(T[] first, T[]... rest) {
        int totalLength = first.length;
        for (T[] array : rest) {
            totalLength += array.length;
        }
        T[] result = Arrays.copyOf(first, totalLength);
        int offset = first.length;
        for (T[] array : rest) {
            System.arraycopy(array, 0, result, offset, array.length);
            offset += array.length;
        }
        return result;
    }
    public static double predictMonthVolumeByWeek(String []item){
        Process proc;
        String MonthNum = String.valueOf(item.length+1);
        String []time = item;
        String []args1 = new String[] { "python", "Liner_For_Month.py",MonthNum};
        String []data = concatAll(args1,time);
        String []a = new String[12];
        try {
            proc = Runtime.getRuntime().exec(data);
            BufferedReader in = new BufferedReader(new InputStreamReader(proc.getInputStream()));
            String line = null;
            int i = 0;
            while ((line = in.readLine()) != null) {
                a[i] = line;
                i++;
                System.out.println(line);
            }
            in.close();
            proc.waitFor();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        double predict = parseDouble(a[0]);
        double error = sqrt(parseDouble(a[1]));
        double max = predict + error;
        //System.out.println("min:"+(predict-error)+" max:"+(predict+error));
        return max;
    }
    public static double predictYearVolumeByYear(String []item){
        Process proc;
        String []args1 = new String[] { "python", "Liner_For_Year.py"};
        String []data = concatAll(args1,item);
        String []a = new String[12];
        try {
            proc = Runtime.getRuntime().exec(data);
            BufferedReader in = new BufferedReader(new InputStreamReader(proc.getInputStream()));
            String line = null;
            int i = 0;
            while ((line = in.readLine()) != null) {
                a[i] = line;
                i++;
                System.out.println(line);
            }
            in.close();
            proc.waitFor();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        double predict = parseDouble(a[0]);
        double error = sqrt(parseDouble(a[1]));
        double max = predict + error;
        //System.out.println("min:"+(predict-error)+" max:"+(predict+error));
        return max;
    }
    public static double predictBreakByPressure(String []item){
        Process proc;
        String []args1 = new String[] { "python", "Pressure.py"};
        String []data = concatAll(args1,item);
        String []a = new String[12];
        try {
            proc = Runtime.getRuntime().exec(data);
            BufferedReader in = new BufferedReader(new InputStreamReader(proc.getInputStream()));
            String line = null;
            int i = 0;
            while ((line = in.readLine()) != null) {
                a[i] = line;
                i++;
                System.out.println(line);
            }
            in.close();
            proc.waitFor();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        double predict = parseDouble(a[0]);
        double error = sqrt(parseDouble(a[1]));
        double max = predict + error;
        //System.out.println("min:"+(predict-error)+" max:"+(predict+error));
        return max;
    }
}
